﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Live.ServerControls.VE;
using System.Reflection;
using System.Drawing;
using System.Globalization;
using System.Xml.Serialization;
using VirtualEarth.GraphProvider;

namespace VirtualEarth
{
    public partial class _Default : System.Web.UI.Page
    {
        List<LatLongWithAltitude> points = new List<LatLongWithAltitude>();

        protected void Page_Load(object sender, EventArgs e)
        {
            DisplayGraph();
        }

        public void DisplayGraph()
        {
            GraphProvider.GraphProviderServiceClient cl = new GraphProvider.GraphProviderServiceClient();
            Graph g = cl.GetGraph();

            foreach (var item in g.Vertices)
            {
                double latitude = 0;
                double longitude = 0;
                //if (!String.IsNullOrEmpty(item.Value locationX) && !String.IsNullOrEmpty(item.locationY))
                //{
                //    latitude = Convert.ToDouble(item.locationX, NumberFormatInfo.InvariantInfo);
                //    longitude = Convert.ToDouble(item.locationY, NumberFormatInfo.InvariantInfo);
                //}
                //else
                //{
                    if (item.Value.Keys.ContainsKey("location"))
                    {
                        Double[] geocode = VisualizeGraphML.GetGeoCode(item.Value.Keys["location"]);
                        latitude = geocode[0];
                        longitude = geocode[1];
                        item.Value.Keys.Add("locationX", latitude.ToString(NumberFormatInfo.InvariantInfo));
                        item.Value.Keys.Add("locationY", longitude.ToString(NumberFormatInfo.InvariantInfo));
                    }
                //}

                //TODO: fix 0.0 cord.
                AddPushPin(latitude, longitude, item.Value.Keys);
            }

            ShapeLayer layer = new ShapeLayer();

            foreach (var item in g.Edges)
            {
                //if (!String.IsNullOrEmpty(item.Source.locationX) && !String.IsNullOrEmpty(item.Source.locationY)
                //    && !String.IsNullOrEmpty(item.Target.locationX) && !String.IsNullOrEmpty(item.Target.locationY)
                //    )
                AddEdge(layer, Convert.ToDouble(g.Vertices[item.Source].Keys["locationX"], NumberFormatInfo.InvariantInfo), Convert.ToDouble(g.Vertices[item.Source].Keys["locationY"], NumberFormatInfo.InvariantInfo),
                        Convert.ToDouble(g.Vertices[item.Target].Keys["locationX"], NumberFormatInfo.InvariantInfo), Convert.ToDouble(g.Vertices[item.Target].Keys["locationY"], NumberFormatInfo.InvariantInfo));
            }
            Map1.AddShapeLayer(layer);
        }

        private void AddPushPin(double latitude, double longitude, Dictionary<string, string> item)
        {
            LatLongWithAltitude point = new LatLongWithAltitude(latitude,
                longitude);
            Shape s = new Shape(ShapeType.Pushpin, point);

            if (item.ContainsKey("gender"))
            {
                if (item["gender"] == "F")
                    s.CustomIcon = "<img src='images/WomanIcon.gif'/>";
                else
                    s.CustomIcon = "<img src='images/ManIcon.gif'/>";
            }
            else
                s.CustomIcon = "<img src='images/ManIcon.gif'/>";

            s.Description = String.Empty;
            //s.Description += item.location;
            //TODO: dump object :) to description
            foreach (KeyValuePair<string, string> item1  in item)
            {
                if (item1.Key != "locationX" && item1.Key != "locationY")
                    s.Description += item1.Key + ": " + item1.Value + "<br/>";
            }
            Map1.AddShape(s);
        }

        private void AddEdge(ShapeLayer layer, double latitude1, double longitude1, double latitude2, double longitude2)
        {
            List<LatLongWithAltitude> points = new List<LatLongWithAltitude>();
            points.Add(
                new LatLongWithAltitude(latitude1,
                longitude1));
            points.Add(
                new LatLongWithAltitude(latitude2,
                longitude2));

            Shape line = new Shape(ShapeType.Polyline, points);
            layer.AddShape(line);
            line.HideIcon();
        }


    }
}
